package com.server.rule.component.triple.complex;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;

import com.server.rule.component.triple.Triple;
import com.server.rule.component.triple.simple.BooleanTriple;
import com.server.rule.component.triple.simple.NominalTriple;
import com.server.rule.component.triple.simple.NumericTriple;
import com.server.rule.component.triple.simple.SimpleTriple;
import com.server.rule.data.Data;

import com.server.rule.data.ExceptionTypeData;
/**
 * Classe abstraite pour les conditions de type complexes (OR,AND,NOT)
 * @author queyroi
 *
 */
public abstract class ComplexTriple extends Triple{
	/**
	 * Liste des sous-conditions
	 */
	protected List<Triple> listeTriple;
	
	public ComplexTriple() {
		listeTriple = new ArrayList<Triple>();
	}
	
	/**
	 * @return the listeTriple
	 */
	public List<Triple> getListeTriple() {
		return listeTriple;
	}

	/**
	 * @param listeTriple the listeTriple to set
	 */
	public void setListeTriple(List<Triple> listeTriple) {
		this.listeTriple = listeTriple;
	}

	
	
	//@Override
	public abstract boolean match(Data data) throws ExceptionTypeData;
	
	public void add(Triple triple){
		listeTriple.add(triple);
	}
	
		
	public abstract Element getXMLElement();
	
	public void generateFromXML(Element xmlElement) {
		
		Iterator iteratorElement = (xmlElement.getChildren()).iterator();
		while(iteratorElement.hasNext()){
			Element elementTemp = (Element) iteratorElement.next();
			Triple tripleTemp = null;			
			if(elementTemp.getAttributeValue("type").equals("NominalTriple")) tripleTemp = new NominalTriple();
			if(elementTemp.getAttributeValue("type").equals("NumericTriple")) tripleTemp = new NumericTriple();
			if(elementTemp.getAttributeValue("type").equals("BooleanTriple")) tripleTemp = new BooleanTriple();
			if(elementTemp.getAttributeValue("type").equals("AndTriple")) tripleTemp = new AndTriple();
			if(elementTemp.getAttributeValue("type").equals("OrTriple")) tripleTemp = new OrTriple();
			if(elementTemp.getAttributeValue("type").equals("ComplementTriple")) tripleTemp = new ComplementTriple();		
			if(tripleTemp!=null){
				tripleTemp.generateFromXML(elementTemp);
				add(tripleTemp);	
			}
			
		}
		
	}
	
	public abstract String getHumanReadable();
	
	public void getUsedVar(List<String> listeUsedVar){
		for(Triple p : listeTriple) p.getUsedVar(listeUsedVar);
	}
	
}
